class PhotosController < ApplicationController

  protect_from_forgery :except => :upload
  #session :cookie_only => false, :only => :upload

  #before_filter :load_member
  skip_before_filter :verify_authenticity_token, :only => :upload
  
  def initialize
    @styleName = "my_photo"
  end

  # GET /photos
  # GET /photos.xml
  def index
    @album = Album.find params[:id]
    @member = Member.find params[:member_id]
    @photos = Photo.paginate_by_album_id params[:id], :page => params[:page], :order => 'updated_at DESC'   

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @photos }
    end
  end

  # GET /photos/1
  # GET /photos/1.xml
  def show
    @photo = Photo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @photo }
    end
  end

  # GET /photos/new
  # GET /photos/new.xml
  def new
    @photo = Photo.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @photo }
    end
  end

  # GET /photos/1/edit
  def edit
    @photo = Photo.find(params[:id])
  end

  # swfupload action set in routes.rb
  def upload
    @photo = Photo.new :album_id => params[:album_id] ,:swf_uploaded_data => params[:Filedata]
    @photo.save!

    # This returns the thumbnail url for handlers.js to use to display the thumbnail
    render :text => @photo.public_filename(:thumb)
  rescue
    render :text => "Error"
  end

  # POST /photos
  # POST /photos.xml
  def create
    album = Album.find 1
    # swfupload action set in routes.rb
    @photo = album.photos.build :uploaded_data => params[:Filedata]
    @photo.save!

    # This returns the thumbnail url for handlers.js to use to display the thumbnail
    render :text => @photo.public_filename(:thumb)
  rescue
    render :text => "Error"
  end

  # PUT /photos/1
  # PUT /photos/1.xml
  def update
    @photo = Photo.find(params[:id])

    respond_to do |format|
      if @photo.update_attributes(params[:photo])
        flash[:notice] = 'Photo was successfully updated.'
        format.html { redirect_to(@photo) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /photos/1
  # DELETE /photos/1.xml
  def destroy
    @photo = Photo.find(params[:id])
    @photo.destroy

    respond_to do |format|
      format.html { redirect_to(photos_url) }
      format.xml  { head :ok }
    end
  end

  protected

  def load_member
    @member = Member.find params[:member_id]
  end
  
end
